import { Pipe, PipeTransform } from '@angular/core';

/**
 * 字符串替换管道
 */
@Pipe({
  name: 'replace'
})
export class ReplacePipe implements PipeTransform {

  transform(value: string, type: string, ellipse?: string) {
    if (value) {
      let start = 0;
      let end = value.length;
      let len = 0;
      switch (type) {
        case 'mobile':
          start = 3;
          end = -4;
          len = 4;
          break;
        case 'email':
          start = 2;
          end = value.indexOf('@');
          len = 4;
          break;
        case 'IDCard':
          start = 4;
          end = -4;
          len = 4;
          break;
        default:
          break;
      }
      if (!ellipse) {
        const arr = [];
        for (let i = 0; i < len; i++) {
          arr.push('*');
        }
        ellipse = arr.join('');
      }
      const prefix = value.substring(0, start);
      const suffix = value.substr(end);
      return prefix + ellipse + suffix;
    }
    return value;
  }

}
